﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SMTC.BO.Report;
using SMTC.BO.BU;
using SMTC.Model.Domain;
using SMTC.Model.Model;

/// <summary>
/// Summary description for ReportDataSource
/// </summary>
public class ReportDataSource
{
    private ReportService _reportServices = new ReportService();
    private BUQueryServices _buSerivces = new BUQueryServices();

    public ReportDataSource()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public List<ReportDataSourceModel> Select(string buname, DateTime start, DateTime end, string PlanVersion, string PUVersion)
    {
        List<SMT_BUModel> list = _buSerivces.GetBUList(buname, "", new DateTime(1900, 1, 1), new DateTime(2999, 12, 31));
        List<ReportDataSourceModel> resultList = new List<ReportDataSourceModel>();
        foreach (var item in list)
        {
            string MonthAlert = "";
            ReportDataSourceModel model = new ReportDataSourceModel();
            model.BU = item.BUName;
            if (this.isCapacityEnough(_reportServices.GetReportCapacity(item.BUName, start, end, PUVersion)
                                     , _reportServices.GetReportPlan(item.BUName, PlanVersion), ref MonthAlert) == true
                )
            {
                model.Discrib = "能力正常";
            }
            else
            {
                if (string.IsNullOrEmpty(MonthAlert))
                {
                    model.Discrib = "没有数据";
                }
                else
                {
                    model.Discrib = "能力不足" + MonthAlert;
                }
            }
            resultList.Add(model);
        }
        return resultList;
    }

    public List<ForcastTeepDomaincs> GetForcast(string buName, DateTime start, DateTime end, string PlanVersion, string PUVersion)
    {
        List<ReportDataSourceModel> capacityList = _reportServices.GetReportCapacity(buName, start, end, PUVersion);
        List<ReportDataSourceModel> planList = _reportServices.GetReportPlan(buName, PlanVersion);
        List<ForcastTeepDomaincs> list = new List<ForcastTeepDomaincs>();

        if (capacityList.Count == 12 && planList.Count == 12)
        {
            for (int index = 0; index < 12; index++)
            {
                ForcastTeepDomaincs domain = new ForcastTeepDomaincs();
                domain.BUName = capacityList[index].BU;
                domain.capacity = Math.Round(float.Parse(capacityList[index].Capacity.ToString()), 0);
                domain.Month = index + 1;
                domain.plan = Math.Round(float.Parse(planList[index].Capacity.ToString()), 0);
                domain.year = DateTime.Now.Year;
                list.Add(domain);
            }
        }

        return list;
    }

    public List<ImportPlanDomain> GetReportPlan(string PlanVersion)
    {
        List<ImportPlanDomain> resultList = new List<ImportPlanDomain>();
        resultList = _reportServices.GetReportPlan(PlanVersion);
        var list = from item in resultList
                   group item by item.PlanVersion into g
                   select new ImportPlanDomain
                   {
                       BU = "Total",
                       m1 = g.Sum(item => item.m1),
                       m2 = g.Sum(item => item.m2),
                       m3 = g.Sum(item => item.m3),
                       m4 = g.Sum(item => item.m4),
                       m5 = g.Sum(item => item.m5),
                       m6 = g.Sum(item => item.m6),
                       m7 = g.Sum(item => item.m7),
                       m8 = g.Sum(item => item.m8),
                       m9 = g.Sum(item => item.m9),
                       m10 = g.Sum(item => item.m10),
                       m11 = g.Sum(item => item.m11),
                       m12 = g.Sum(item => item.m12)
                   };
        List<ImportPlanDomain> totalList = list.Cast<ImportPlanDomain>().ToList<ImportPlanDomain>();
        if (totalList != null && totalList.Count > 0)
        {
            resultList.Add(totalList[0]);
        }
        return resultList;

    }

    public List<ImportPlanDomain> GetReportPlan_T(string PlanVersion)
    {
        List<ImportPlanDomain> resultList = new List<ImportPlanDomain>();
        resultList = _reportServices.GetReportPlan_T(PlanVersion);
        var list = from item in resultList
                   group item by item.PlanVersion into g
                   select new ImportPlanDomain
                   {
                       BU = "Total",
                       m1 = g.Sum(item => item.m1),
                       m2 = g.Sum(item => item.m2),
                       m3 = g.Sum(item => item.m3),
                       m4 = g.Sum(item => item.m4),
                       m5 = g.Sum(item => item.m5),
                       m6 = g.Sum(item => item.m6),
                       m7 = g.Sum(item => item.m7),
                       m8 = g.Sum(item => item.m8),
                       m9 = g.Sum(item => item.m9),
                       m10 = g.Sum(item => item.m10),
                       m11 = g.Sum(item => item.m11),
                       m12 = g.Sum(item => item.m12)
                   };
        List<ImportPlanDomain> totalList = list.Cast<ImportPlanDomain>().ToList<ImportPlanDomain>();
        if (totalList != null && totalList.Count > 0)
        {
            resultList.Add(totalList[0]);
        }
        return resultList;

    }

    public List<ImportPlanDomain> GetReportCapacity(DateTime start, DateTime end, string PUVersion)
    {
        List<ImportPlanDomain> resultList = new List<ImportPlanDomain>();
        resultList = _reportServices.GetReportCapacity(start, end, PUVersion);
        var list = from item in resultList
                   group item by item.PlanVersion into g
                   select new ImportPlanDomain
                   {
                       BU = "Total",
                       m1 = g.Sum(item => item.m1),
                       m2 = g.Sum(item => item.m2),
                       m3 = g.Sum(item => item.m3),
                       m4 = g.Sum(item => item.m4),
                       m5 = g.Sum(item => item.m5),
                       m6 = g.Sum(item => item.m6),
                       m7 = g.Sum(item => item.m7),
                       m8 = g.Sum(item => item.m8),
                       m9 = g.Sum(item => item.m9),
                       m10 = g.Sum(item => item.m10),
                       m11 = g.Sum(item => item.m11),
                       m12 = g.Sum(item => item.m12)
                   };
        List<ImportPlanDomain> totalList = list.Cast<ImportPlanDomain>().ToList<ImportPlanDomain>();
        if (totalList != null && totalList.Count > 0)
        {
            resultList.Add(totalList[0]);
        }
        return resultList;
    } 



    private bool isCapacityEnough(List<ReportDataSourceModel> capacity, List<ReportDataSourceModel> plan, ref string strMonth)
    {
        bool isEnough = true;

        for (int index = 0; index < 12; index++)
        {
            try
            {
                if (capacity[index].Capacity < plan[index].Capacity)
                {
                    isEnough = false;
                    strMonth += (index + 1).ToString() + ",";
                }
            }
            catch
            {
                isEnough = false;
                break;
            }
        }
        if (!string.IsNullOrEmpty(strMonth))
        {
            strMonth = strMonth.Substring(0, strMonth.Length - 1);
            strMonth = ":" + strMonth;
        }
        return isEnough;
    }
}